package com.doudou.casebook.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.doudou.casebook.R;


public class BeiZhuDialog extends Dialog implements View.OnClickListener {
    EditText et;
    Button cancelBtn, ensureBtn;
    OnEnsureListener onEnsureListener;

    public BeiZhuDialog(@NonNull Context context) {
        super(context);
        setContentView(R.layout.dialog_beizhu);
        et = findViewById(R.id.dialog_beizhu_et);
        cancelBtn = findViewById(R.id.dialog_beizhu_btn_cancel);
        ensureBtn = findViewById(R.id.dialog_beizhu_btn_ensure);
        cancelBtn.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
    }

    public interface OnEnsureListener {
        public void OnEnsure();
    }

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    @Override
    public void onClick(View v) {
        if (R.id.dialog_beizhu_btn_cancel == v.getId()) {
            cancel();
        } else if (R.id.dialog_beizhu_btn_ensure == v.getId()) {
            onEnsureListener.OnEnsure();
        }
    }

    //获取输入数据的方法
    public String getEditText(){
        return et.getText().toString().trim();
    }
    //设置dialog尺寸与屏幕尺寸一样宽
    public void setDialogSize(){
        //获取窗口对象
        Window window = getWindow();
        //获取对象参数
        assert window != null;
        WindowManager.LayoutParams wlp = window.getAttributes();
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = d.getWidth();
        wlp.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
        handler.sendEmptyMessageDelayed(1,100);
    }

 @SuppressLint("HandlerLeak")
 Handler handler = new Handler() {

     @Override
     public void handleMessage(@NonNull Message msg) {
         // 自动弹出软键盘
         InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
         inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
     }
 };
}
